package com.onion.codegen.utils;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.apache.commons.lang3.StringUtils;

import com.onion.codegen.bean.DbTableProperties;
import com.onion.codegen.bean.JavaBeanProperties;
import com.onion.codegen.bean.GeneratorProperties;
import com.onion.codegen.db.JavaSQLType;

public class TableToJavaEntityUtil {

	private static List<JavaBeanProperties> tableToJava(List<DbTableProperties> tablePropertiesList,GeneratorProperties javaEntityProperties) {
		List<JavaBeanProperties> genCodeList = tableChangeToJava(tablePropertiesList,javaEntityProperties);
		readyToGenJava(genCodeList,javaEntityProperties);
		return genCodeList;
	}
	
	public static List<JavaBeanProperties> tableChangeToJava(List<DbTableProperties> tablePropertiesList,GeneratorProperties javaEntityProperties) {

		List<JavaBeanProperties> genCodeList = new ArrayList<JavaBeanProperties>();
		
		for (DbTableProperties item : tablePropertiesList) {
			JavaBeanProperties genCodeProperties = new JavaBeanProperties();
			List<String> columnNames = new ArrayList<String>();
			List<String> dbcolumnNames = new ArrayList<String>();
			List<String> columnTypes = new ArrayList<String>();
			List<String> dbcolumnTypes =new ArrayList<String>();	//数据库的数据类型
			List<String> columnComments = new ArrayList<String>();
			Set<String> imports = new HashSet<>();

			StringBuffer classNameBuffer = new StringBuffer();
			String[] tableSplit = item.getTableName().split("_");
			for (String tableChar : tableSplit) {
				classNameBuffer.append(tableChar.substring(0, 1).toUpperCase()).append(tableChar.substring(1, tableChar.length()));
			}
			genCodeProperties.setDbTable(item.getTableName());
			genCodeProperties.setJavaName(classNameBuffer.toString());
			// 实体类中的字段属性名称
			for (String columnItem : item.getColumnNames()) {
				StringBuffer columnNameBuffer = new StringBuffer();
				String[] columnSplit = columnItem.split("_");
				int length = columnSplit.length;
				for (int i = 0; i < length; i++) {
					String columnChar = columnSplit[i];
					if (i == 0) {
						columnNameBuffer.append(columnChar.substring(0, 1).toLowerCase()).append(columnChar.substring(1, columnChar.length()));
					} else {
						columnNameBuffer.append(columnChar.substring(0, 1).toUpperCase()).append(columnChar.substring(1, columnChar.length()));
					}
				}
				dbcolumnNames.add(columnItem);
				columnNames.add(columnNameBuffer.toString());
			}
			genCodeProperties.setColumnNames(columnNames);
			// 数据库中的字段类型
			for (String columnType : item.getColumnTypes()) {
				String javaImport = JavaSQLType.sqlTypeImportMap.get(columnType);
				if (javaImport != null) {
					imports.add(javaImport);
				}
				String javaType = JavaSQLType.sqlTypeColumnMap.get(columnType);
				if (javaType != null) {
					columnTypes.add(javaType);
				}else {
					System.out.println("数据库表"+item.getTableName()+"数据类型："+columnType+" 未匹配到Java类型");
				}
				dbcolumnTypes.add(columnType);
			}
			// 数据库中的字段注释
			for (String columnComment : item.getColumnComments()) {
				columnComments.add(columnComment);
			}
			genCodeProperties.setPackages(javaEntityProperties.getPackageName());
			
			//添加父类的import导入
			if(javaEntityProperties.getSuperClass()!=null) {
				imports.add(javaEntityProperties.getSuperClass().substring(0,javaEntityProperties.getSuperClass().length()-5));
			}
			genCodeProperties.setJavaClassDescribe(item.getTableDescribe());
			genCodeProperties.setImports(imports);
			genCodeProperties.setColumnNames(columnNames);
			genCodeProperties.setDbcolumnNames(dbcolumnNames);
			genCodeProperties.setColumnTypes(columnTypes);
			genCodeProperties.setDbcolumnTypes(dbcolumnTypes);
			genCodeProperties.setColumnComments(columnComments);
			genCodeList.add(genCodeProperties);
		}
		return genCodeList;
	}
	
	/**
	 * @param genCodeList
	 */
	public static void readyToGenJava(List<JavaBeanProperties> genCodeList,GeneratorProperties javaEntityProperties) {
		for(JavaBeanProperties item:genCodeList) {
			StringBuffer content = new StringBuffer();
			content.append("package ").append(item.getPackages()).append(";").append("\n\n");;
			Set<String> imports = item.getImports();
			for(String imp:imports) {
				content.append("import ").append(imp).append(";").append("\n");
			}
			content.append("/**\n");
			content.append("  * @author ").append(javaEntityProperties.getAuthor()==null?"":javaEntityProperties.getAuthor()).append("\n");
			content.append("  * @describe ").append(item.getJavaClassDescribe()).append("\n");
			content.append("  */").append("\n");
			if(StringUtils.isEmpty(javaEntityProperties.getSuperClass())){
				content.append("public class ").append(item.getJavaName()).append(" {").append("\n");
			}else {
				String[] split = javaEntityProperties.getSuperClass().split("[.]");
				String superClassName = split[split.length-2];
				content.append("public class ").append(item.getJavaName()).append(" extends ").append(superClassName).append(" {").append("\n");
			}
			content.append("\n");
			List<String> columnNames = item.getColumnNames();
			List<String> columnTypes = item.getColumnTypes();
			List<String> columnComments = item.getColumnComments();
			int columnNameLength = columnNames.size();
			for(int i=0;i<columnNameLength;i++) {
				content.append("    /**").append("\n");
				content.append("     * ").append(columnComments.get(i)).append("\n");
				content.append("     */").append("\n");
				content.append("    private ").append(columnTypes.get(i)).append(" ").append(columnNames.get(i)).append(";\n");
			}
			content.append("\n\n");
			for(int i=0;i<columnNameLength;i++) {
				String column = columnNames.get(i);
				char secondChar = column.charAt(1);
				String tmpField = "";
				if(Character.isUpperCase(secondChar)) {
					tmpField = column;
				}else {
					tmpField = column.substring(0, 1).toUpperCase()+column.substring(1, column.length());
				}
				
				content.append("    public ").append(columnTypes.get(i)).append(" ").append("get").append(tmpField).append("() {\n");
				content.append("	    return ").append(column).append(";\n");
				content.append("    }\n\n");
				
				content.append("    public void set").append(tmpField).append("(").append(columnTypes.get(i)).append(" ").append(column).append(")").append("{\n");
				content.append("   	    this.").append(column).append("=").append(column).append(";\n");
				content.append("    }\n\n");
			}
			content.append("}");
			
			item.setJavaContent(content.toString());
		}
	}
}